package com.pe1n.yangtaopai.view.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.view.fragment.AmountFragment;
import com.pe1n.yangtaopai.view.fragment.HomePageFragment;
import com.pe1n.yangtaopai.view.fragment.MyFragment;
import com.pe1n.yangtaopai.view.fragment.ShoppingFragment;

public class MainActivity extends AppCompatActivity {
    private   RadioGroup mRadioGroup_main;//下面的导航栏
    private FrameLayout mFrameLayout_main;//动态添加Fragment的容器
    private RadioButton mHomePageButton;//首页
    private RadioButton mShoppingButton;//购物车
    private RadioButton mAmountButton;//日本销量
    private RadioButton mMyButton;//我
    private FragmentManager mFragmentManager;//fragment的管理
    private FragmentTransaction mFragmentTransaction;
    private AmountFragment mAmountFragment;
    private HomePageFragment mHomePageFragment;
    private MyFragment mMyFragment;
    private ShoppingFragment mShoppingFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFrameLayout();
       initView();

    }
    private void initFrameLayout() {
        mFragmentManager = getSupportFragmentManager();

    }
    private void initView() {
        mFrameLayout_main = (FrameLayout) findViewById(R.id.frameLayout_main);
        mRadioGroup_main = (RadioGroup) findViewById(R.id.radioGroup_main);
        mHomePageButton = (RadioButton) findViewById(R.id.homePageFragment_main);
        mAmountButton = (RadioButton) findViewById(R.id.amountFragment_main);
        mShoppingButton = (RadioButton) findViewById(R.id.shoppingFragment_main);
        mMyButton = (RadioButton) findViewById(R.id.myFragment_main);
        mRadioGroup_main.check(R.id.homePageFragment_main);
        mFragmentTransaction = mFragmentManager.beginTransaction();

        mHomePageFragment = new HomePageFragment();
        mFragmentTransaction.add(R.id.frameLayout_main,   mHomePageFragment, "HomePageFragment");
        mFragmentTransaction.show(mHomePageFragment);

        mAmountFragment = new AmountFragment();
        mFragmentTransaction.add(R.id.frameLayout_main, mAmountFragment, "AmountFragment");
        mFragmentTransaction.hide(mAmountFragment);

        mShoppingFragment = new ShoppingFragment();
        mFragmentTransaction.add(R.id.frameLayout_main,  mShoppingFragment, "ShoppingFragment");
        mFragmentTransaction.hide( mShoppingFragment);

        mMyFragment= new MyFragment();
        mFragmentTransaction.add(R.id.frameLayout_main,  mMyFragment, "  MyFragment");
        mFragmentTransaction.hide(mMyFragment);
        mFragmentTransaction.commit();

        mRadioGroup_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.homePageFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.show(mHomePageFragment);
                        mFragmentTransaction.hide(mAmountFragment);
                        mFragmentTransaction.hide(mShoppingFragment);
                        mFragmentTransaction.hide(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                    case R.id.amountFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.hide(mHomePageFragment);
                        mFragmentTransaction.show(mAmountFragment);
                        mFragmentTransaction.hide(mShoppingFragment);
                        mFragmentTransaction.hide(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                    case R.id.shoppingFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.hide(mHomePageFragment);
                        mFragmentTransaction.hide(mAmountFragment);
                        mFragmentTransaction.show(mShoppingFragment);
                        mFragmentTransaction.hide(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                    case R.id.myFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.hide(mHomePageFragment);
                        mFragmentTransaction.hide(mAmountFragment);
                        mFragmentTransaction.hide(mShoppingFragment);
                        mFragmentTransaction.show(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                }
            }
        });
    }

}
